#include<iostream>
using namespace std;
const int N = 1005;
int father[N];
int n, m;
void Init()
{
	for (int i = 0; i < n; i++) {
		father[i + 1] = i + 1;
	}
}
int find(int x)
{
	/*if (x == father[x]) {
		return x;
	}
	else {
		father[x] = find(father[x]);
	}*/
	while (x != father[x]) {
		x = father[x];
	}
	return x;
}
void Merge(int x, int y)
{
	int fx = find(x);
	int fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
	}
}
int main()
{
	while (cin >> n >> m) {
		if (n == 0) {
			break;
		}
		Init();
		for (int i = 0; i < m; i++) {
			int x, y;
			cin >> x >> y;
			Merge(x, y);
		}
		int count = 0;
		for (int i = 1; i <= n; i++) {
			if (father[i] == i) {
				count++;
			}
		}
		cout << count - 1 << endl;
	}
	
	return 0;
}